package com.ruoyi.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.base.domain.SyQj;
import com.ruoyi.base.domain.bo.SyQjBo;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.TreeBuildUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.base.domain.vo.SyQjVo;
import com.ruoyi.base.mapper.SyQjMapper;
import com.ruoyi.base.service.ISyQjService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试验器具Service业务层处理
 *
 * @author zyb
 * @date 2024-04-12
 */
@RequiredArgsConstructor
@Service
public class SyQjServiceImpl implements ISyQjService {

    private final SyQjMapper baseMapper;

    /**
     * 查询试验器具
     */
    @Override
    public SyQjVo queryById(Long syQjId){
        return baseMapper.selectVoById(syQjId);
    }


    /**
     * 查询试验器具列表
     */
    @Override
    public List<SyQjVo> queryList(SyQjBo bo) {
        LambdaQueryWrapper<SyQj> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SyQj> buildQueryWrapper(SyQjBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SyQj> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, SyQj::getParentId, bo.getParentId());
        lqw.like(StringUtils.isNotBlank(bo.getSyQjName()), SyQj::getSyQjName, bo.getSyQjName());
        lqw.eq(bo.getSort() != null, SyQj::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增试验器具
     */
    @Override
    public Boolean insertByBo(SyQjBo bo) {
        SyQj add = BeanUtil.toBean(bo, SyQj.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSyQjId(add.getSyQjId());
        }
        return flag;
    }

    /**
     * 修改试验器具
     */
    @Override
    public Boolean updateByBo(SyQjBo bo) {
        SyQj update = BeanUtil.toBean(bo, SyQj.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }


    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SyQj entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试验器具
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询器具树结构信息
     *
     * @param qj 器具信息
     * @return 器具树信息集合
     */
    @Override
    public List<Tree<Long>> selectQjTreeList(SyQj qj) {
        List<SyQj> qjs = this.selectQjList(qj);
        return buildQjTreeSelect(qjs);
    }

    /**
     * 查询子集
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<SyQjVo> pageSub(SyQjBo bo, PageQuery pageQuery) {
        if (ObjectUtils.isEmpty(bo.getDeptId())) {
            throw new ServiceException("请选择试验室");
        }
        if (ObjectUtils.isEmpty(bo.getParentId())) {
            return TableDataInfo.build(new Page<>());
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("del_flag", "0");
        wrapper.eq("parent_id", bo.getParentId());
        wrapper.eq("dept_id", bo.getDeptId());
        wrapper.orderByAsc("create_time");
        IPage page = baseMapper.selectVoPage(pageQuery.build(), wrapper);
        return TableDataInfo.build(page);
    }

    /**
     * 器具列表
     * @param bo
     * @return
     */
    @Override
    public List<SyQjVo> qjList(SyQjBo bo) {
        LambdaQueryWrapper<SyQj> wrapper = new LambdaQueryWrapper();
        wrapper.ne(SyQj::getParentId, 0L);
        wrapper.eq(SyQj::getDelFlag, 0);
        wrapper.orderByAsc(SyQj::getCreateTime);
        List list = baseMapper.selectVoList(wrapper);
        return list;
    }

    public List<SyQj> selectQjList(SyQj qj) {
        LambdaQueryWrapper<SyQj> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SyQj::getDelFlag, "0")
            .eq(ObjectUtil.isNotNull(qj.getSyQjId()), SyQj::getSyQjId, qj.getSyQjId())
            .eq(ObjectUtil.isNotNull(qj.getParentId()), SyQj::getParentId, qj.getParentId())
            .like(StringUtils.isNotBlank(qj.getSyQjName()), SyQj::getSyQjName, qj.getSyQjName())
            .orderByAsc(SyQj::getParentId)
            .orderByAsc(SyQj::getSort);
        return baseMapper.selectList(lqw);
    }

    public List<Tree<Long>> buildQjTreeSelect(List<SyQj> qjs) {
        if (CollUtil.isEmpty(qjs)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(qjs, (qj, tree) ->
            tree.setId(qj.getSyQjId())
                .setParentId(qj.getParentId())
                .setName(qj.getSyQjName())
                .setWeight(qj.getSort()));
    }
}
